package com.ncloud.resman.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ncloud.common.utils.DateUtils;
import com.ncloud.common.utils.PageHelperUtils;
import com.ncloud.common.utils.excel.ExcelUtil;
import com.ncloud.resman.entity.Brand;
import com.ncloud.resman.entity.RepairRecord;
import com.ncloud.resman.mapper.RepairRecordMapper;
import com.ncloud.resman.service.RepairRecordService;

/**
 * user:sundelong  date:2017年8月11日
 */
@Service
public class RepairRecordImpl implements RepairRecordService {
	@Resource
	RepairRecordMapper repairRecordMapper;
	
	public List<RepairRecord> repairList=new ArrayList<RepairRecord>();

	@Override
	public Object getRepairList(JSONObject json) {
		Object object = null;		
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<RepairRecord> list=repairRecordMapper.getRepairList();
			PageInfo pageInfo = new PageInfo(list);
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else{
			List<RepairRecord> list=repairRecordMapper.getRepairList();
			object = JSON.parseArray(JSON.toJSONString(list));
		}		
		return object;
	}

	@Override
	public Object getRepairInfoBySnId(JSONObject jsonInfo) {
		String eqptType=jsonInfo.getString("eqptType");
		String snId=jsonInfo.getString("snId");
		String repairState=jsonInfo.getString("repairState");
		String beginDate=jsonInfo.getString("beginDate");
		String endDate=jsonInfo.getString("endDate");
		Object object = null;		
		if(PageHelperUtils.isPage(jsonInfo)){
			PageHelperUtils.setPageParam(jsonInfo);
			List<RepairRecord> list=repairRecordMapper.getRepairInfoBySnId(eqptType, snId, repairState, beginDate, endDate);		
			PageInfo pageInfo = new PageInfo(list);
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else{
			List<RepairRecord> list=repairRecordMapper.getRepairInfoBySnId(eqptType, snId, repairState, beginDate, endDate);		
			object = JSON.parseArray(JSON.toJSONString(list));
		}
		return object;
	}

	@Override
	public int insertRepair(RepairRecord repairRecord) {		
		return repairRecordMapper.insertRepair(repairRecord);
	}

	@Override
	public int updateRepair(JSONObject jsonInfo) {
		String eqptType=jsonInfo.getString("eqptType");
		String snId=jsonInfo.getString("snId");
		String repairState=jsonInfo.getString("repairState");
		String solution=jsonInfo.getString("solution");
		String updateUser=jsonInfo.getString("updateUser");
		String id=jsonInfo.getString("id");
		return repairRecordMapper.updateRepair(eqptType, snId, repairState, solution, updateUser,id);

			}

	@Override
	public int deleteRepair(JSONObject jsonInfo) {
		String id=jsonInfo.getString("id");
		return repairRecordMapper.deleteRepair(id);
	}
	
	public void exportRepair(JSONObject json,HttpServletResponse response) throws IOException{
		List<RepairRecord> listInfo=new ArrayList<RepairRecord>();
		String exportType = json.getString("exportType");
		String id=json.getString("id");
		String eqptType=json.getString("eqptType");
		String snId=json.getString("snId");
		if(snId==null||snId==""){
			snId="";
		}
		String repairState=json.getString("repairState");
		String beginDate=json.getString("beginDate");
		String endDate=json.getString("endDate");
		listInfo=repairRecordMapper.getRepairInfoBySnId(eqptType,snId,repairState,beginDate,endDate);
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName="保修清单_"+sdf.format(date)+".xls";
		OutputStream os = null;
		response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"),"ISO-8859-1"));
        os = response.getOutputStream();
        Map<String, String> map = new HashMap<String, String>();
        map.put("title", "报修列表");
        map.put("total", listInfo.size()+" 条");
        map.put("date", DateUtils.getDate());
        ExcelUtil.getInstance().exportObj2ExcelByTemplate(map,"repair-infos-template.xls",os,listInfo,RepairRecord.class,true);
	}
	
}
